package com.aidex.system.controller;

import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.system.domain.VCsisBusinessRecord;
import com.aidex.system.domain.Waybill;
import com.aidex.system.domain.vo.CountInfoVO;
import com.aidex.system.domain.vo.WaybillQueryVO;
import com.aidex.system.domain.vo.WaybillQueryVO1;
import com.aidex.system.domain.vo.WaybillVO;
import com.aidex.system.mapper.VCsisBusinessRecordMapper;
import com.aidex.system.mapper.VInternationalRecordMapper;
import com.aidex.system.mapper.WaybillMapper;
import com.aidex.system.service.VCsisBusinessAsdService;
import com.aidex.system.service.WaybillService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 运单管理Controller
 * @author jmy
 * @email jmyinjg@163.com
 * @date 2021-10-04
 */
@RestController
@RequestMapping("/system/waybill")
public class WaybillController extends BaseController
{
    @Autowired
    private WaybillService waybillService;
    @Resource
    private WaybillMapper waybillMapper;
    @Autowired
    private VCsisBusinessAsdService vCsisBusinessAsdService;
    @Resource
    private VCsisBusinessRecordMapper vCsisBusinessRecordMapper;
    @Resource
    private VInternationalRecordMapper vInternationalRecordMapper;

    /**
     * 查询运单管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:waybill:list')")
    @GetMapping("/list")
    public R<PageInfo> list(Waybill waybill, HttpServletRequest request, HttpServletResponse response)
    {
        waybill.setPage(new PageDomain(request, response));
        return R.data(waybillService.findPage(waybill));
    }

    /**
     * 查询运单
     * @param query
     * @return
     */
    @GetMapping("/page")
    public R<PageInfo> page(WaybillQueryVO query) {
        long start = System.currentTimeMillis();
        logger.info("开始计时");
        PageDomain page = new PageDomain();
        page.setPageNum(query.getPageNum());
        page.setPageSize(query.getPageSize());
        query.setPage(page);
        PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
        List<WaybillVO> list = waybillMapper.page(query);
        long end1 = System.currentTimeMillis();
        logger.info("查询结束: {}", end1 - start);
        if (list.size() == 0) {
            return R.data(new PageInfo(list));
        }
        List<VCsisBusinessRecord> records = vCsisBusinessRecordMapper.listByAwbNos(list.stream().map(WaybillVO::getWaybillNo)
                .filter(Objects::nonNull).distinct().collect(Collectors.toList()));
        Map<String, VCsisBusinessRecord> recordMap = records.stream()
                .collect(Collectors.toMap(VCsisBusinessRecord::getAwbno, Function.identity()));
        logger.info("查询结束2: {}", System.currentTimeMillis() - end1);
        for (WaybillVO waybill : list) {
            VCsisBusinessRecord record1 = recordMap.get(waybill.getWaybillNo());
            if (record1 != null) {
                waybill.setCheckPoint(record1.getCheckpoint());
                waybill.setSecurityPcs(record1.getSecuritypcs());
            }
        }
        return R.data(new PageInfo(list));
    }

    /**
     * 查询运单
     * @param query
     * @return
     */
    @GetMapping("/pageexport")
    public R pageexport(WaybillQueryVO1 query) {
        List<WaybillVO> list = waybillMapper.page1(query);
        List<VCsisBusinessRecord> records = vCsisBusinessRecordMapper.listByAwbNos(list.stream().map(WaybillVO::getWaybillNo)
                .filter(Objects::nonNull).distinct().collect(Collectors.toList()));
        Map<String, VCsisBusinessRecord> recordMap = records.stream()
                .collect(Collectors.toMap(VCsisBusinessRecord::getAwbno, Function.identity()));
        for (WaybillVO waybill : list) {
            VCsisBusinessRecord record1 = recordMap.get(waybill.getWaybillNo());
            if (record1 != null) {
                waybill.setCheckPoint(record1.getCheckpoint());
                waybill.setSecurityPcs(record1.getSecuritypcs());
            }
        }
        ExcelUtil<WaybillVO> util = new ExcelUtil<>(WaybillVO.class);
        return util.exportExcel(list, "waybill");
    }

    @GetMapping("count")
    public R<List<CountInfoVO>> countInfo(WaybillQueryVO query) {
        return R.data(waybillMapper.countInfo(query));
    }
    @GetMapping("countexcel")
    public R countexcel(WaybillQueryVO query) {
        List<CountInfoVO> countInfoVOS = waybillMapper.countInfo(query);
        ExcelUtil<CountInfoVO> util = new ExcelUtil<CountInfoVO>(CountInfoVO.class);
        return util.exportExcel(countInfoVOS, "countexcel");
    }

    @GetMapping("countinfo")
    public R<List<CountInfoVO>> countInfoBy(WaybillQueryVO query) {
        return R.data(waybillMapper.countInfoBy(query));
    }

    @GetMapping("countinfoexcel")
    public R countinfoexcel(WaybillQueryVO query) {
        List<CountInfoVO> countInfoVOS = waybillMapper.countInfoBy(query);
        ExcelUtil<CountInfoVO> util = new ExcelUtil<CountInfoVO>(CountInfoVO.class);
        return util.exportExcel(countInfoVOS, "countinfoexcel");
    }

    /**
     * 获取运单管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:waybill:query')")
    @GetMapping(value = "/{id}")
    public R<Waybill> detail(@PathVariable("id") String id)
    {
        return R.data(waybillService.get(id));
    }

    /**
     * 新增运单管理
     */
//    @PreAuthorize("@ss.hasPermi('system:waybill:add')")
    @Log(title = "运单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  Waybill waybill)
    {
        return R.status(waybillService.save(waybill));
    }

    /**
     * 修改运单管理
     */
//    @PreAuthorize("@ss.hasPermi('system:waybill:edit')")
    @Log(title = "运单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Waybill waybill)
    {
        return R.status(waybillService.save(waybill));
    }

    /**
     * 删除运单管理
     */
//    @PreAuthorize("@ss.hasPermi('system:waybill:remove')")
    @Log(title = "运单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(waybillService.deleteWaybillByIds(ids));
    }


    /**
     * 导出运单管理列表
     */
//    @PreAuthorize("@ss.hasPermi('system:waybill:export')")
    @Log(title = "运单管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Waybill waybill)
    {
        List<Waybill> list = waybillService.findList(waybill);
        ExcelUtil<Waybill> util = new ExcelUtil<Waybill>(Waybill.class);
        return util.exportExcel(list, "waybill");
    }

}
